var express = require("express");
var router = express.Router();
var banners = require("./banners/banners.js");
var homedata = require("./homedata/homedata.js")
var blueBerryJam = require("./blueberrypai/blueberrypai.js")
var blueBerryJam2 = require("./blueberrypai/blueberrypai2.js")
var blueBerryJam3 = require("./blueberrypai/blueberrypai3.js")
var blueBerrydetails = require("./blueberrypai/blueberrydetails.js")
var community = require("./community/community.js");
var chatList = require("./chatlist/chatlist.js");
var chatListTwo = require("./chatlist/chatlisttwo.js");
var chatListThree = require("./chatlist/chatlistthree.js");
var chatListFour = require("./chatlist/chatlistfour.js");
var listen = require("./listen/listen.js");
var mySQL = require("./connmysql/ConnMysql.js")
var url = require("url");
var player = require("./listenDetails/player.js");
var comment = require("./listenDetails/comment.js");
var songplay = require("./songlist/songplay.js");
var songcon = require("./songlist/songcon.js");

// 轮播图数据
router.get("/api/banners",function(req,res){
  res.send(banners)
})
// 首页具体数据
router.get("/api/homedata",function(req,res){
  res.send(homedata)
})
router.get("/api/blueBerryJam",function(req,res){
  res.send(blueBerryJam)
})
router.get("/api/blueBerryJam2",function(req,res){
  res.send(blueBerryJam2)
})
router.get("/api/blueBerryJam3",function(req,res){
  res.send(blueBerryJam3)
})
router.get("/api/blueBerrydetails",function(req,res){
  res.send(blueBerrydetails)
})
router.get("/api/community",function(req,res){
  res.send(community)
})

router.get("/api/chatList",function(req,res){
	res.send(chatList)
})
router.get("/api/chatListtwo",function(req,res){
	res.send(chatListTwo)
})
router.get("/api/chatListthree",function(req,res){
	res.send(chatListThree)
})
router.get("/api/chatListfour",function(req,res){
	res.send(chatListFour)
})

router.get("/api/listen",function(req,res){
  res.send(listen)
})
router.get("/api/songplay",function(req,res){
  res.send(songplay)
})
router.get("/api/songcon",function(req,res){
  res.send(songcon)
})
router.get("/api/player",function(req,res){
  res.send(player)
})
router.get("/api/comment",function(req,res){
  res.send(comment)
})

// 连接数据库并登录

router.get("/api/login",function(req,res){
  var username = url.parse(req.url,true).query.username;
  var password = url.parse(req.url,true).query.password;
  var sql = "select * from student where username=? and password=?";
  var currentParams = [username,password];
  mySQL.conn(sql,currentParams,function(data){
    console.log(data)
    if(data.length>0){
      res.send({
        success:"true",
        code:200
      });
    }else{
      res.send({
        success:"false",
        code:400
      })
    }
  })
})
// 连接数据库注册账号
router.get("/api/register",function(req,res){

  var username = url.parse(req.url,true).query.username;
  var password = url.parse(req.url,true).query.password;

  var sql = "insert into student values(null,?,?)";
  var currentParams = [username,password];
  mySQL.conn(sql,currentParams,function(data){
    if(data.affectedRows){
      res.send({
        success:"true",
        code:200
      });
    }else{
      res.send({
        success:"false",
        code:400
      })
    }
  })
})

// 连接数据库判断用户名是否存在
router.get("/api/check",function(req,res){
  var username = url.parse(req.url,true).query.username;
  var sql = "select * from student where username=?";
  var currentParams = [username];
  mySQL.conn(sql,currentParams,function(data){
    console.log(data)
    if(data.length>0){
      res.send({
        success:"true",
        code:200
      });
    }else{
      res.send({
        success:"false",
        code:400
      })
    }
  })
})

module.exports = router;
